//
//  GuideCollectionViewCell.m
//  项目一
//
//  Created by 王鑫 on 15/11/13.
//  Copyright (c) 2015年 王鑫. All rights reserved.
//

#import "GuideCollectionViewCell.h"
#import "GuideTableViewCell.h"
#import "GuideModel.h"
#import "AFNetworking.h"
#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height

@implementation GuideCollectionViewCell
#pragma mark - Method dealloc
- (void)dealloc {
    [_guideTableView release];
    [_guideArray release];
    [super dealloc];
}
#pragma mark - Method initWithFrame
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
    }
    return self;
}
#pragma mark - Method createTableView
- (void)createTableView {
    self.guideTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.contentView addSubview:self.guideTableView];
    self.guideTableView.dataSource = self;
    self.guideTableView.delegate = self;
    [_guideTableView release];
    self.guideArray = [NSMutableArray array];
    
    [self createRefreshUpOrDown];
}
#pragma mark - Method height For Row At IndexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 110;
}
#pragma mark - Method number Of Rows In Section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.guideArray.count;
}
#pragma mark - Method cell For Row At IndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *guideReuse = @"guideReuse";
    GuideTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:guideReuse];
    if (!cell) {
        cell = [[GuideTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:guideReuse];
    }
    GuideModel *guideTemp = self.guideArray[indexPath.row];
    cell.model = guideTemp;
    return cell;
}
#pragma mark - Method layout Subviews
- (void)layoutSubviews {
    [super layoutSubviews];
    self.guideTableView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
}
#pragma mark - Method createRefreshUpOrDown
- (void)createRefreshUpOrDown {
    self.guideTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageNext = @"0";
        [self createData];
        
    }];
    self.guideTableView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self.pageNext = [[self.guideArray lastObject] lastid];
        [self createData];
    }];
    [self.guideTableView.header beginRefreshing];
}
#pragma mark - Method createData
- (void)createData {
//    NSString *string = @"http://app.api.autohome.com.cn/autov5.0.0/news/newslist-pm1-c0-nt60-p1-s30-l%@.json";
//    NSString *strUrl = [NSString stringWithFormat:@"%@%ld", string, se]
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://app.api.autohome.com.cn/autov5.0.0/news/newslist-pm1-c0-nt60-p1-s30-l0.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSMutableArray *array = dic[@"result"][@"newslist"];
        NSLog(@"%@", self.pageNext);
        self.tempArr = [NSMutableArray array];
        for (NSDictionary *tempDic in array) {
            GuideModel *model = [[GuideModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.tempArr addObject:model];
            [model release];
        }
        
        if ([self.pageNext isEqualToString:@"0"]) {
            self.guideArray = self.tempArr;
            [self.guideTableView.header endRefreshing];
        } else {
            [self.guideArray addObjectsFromArray:self.tempArr];
            [self.guideTableView.footer endRefreshing];
        }
        
        [self.guideTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
        [self.guideTableView.header endRefreshing];
        [self.guideTableView.footer endRefreshing];
    }];
}
#pragma mark - Method did Select Row At IndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 设置代理人的执行方法
    [self.delegate GuidePushToWeb:self.guideArray[indexPath.row]];
    
}

@end
